package com.vfa.data;

import java.util.ArrayList;

import com.vfa.test.CatagoryE;
import com.vfa.test.QuestionData;
import com.vfa.vocaburay.R;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;

public class SettingActivity extends Activity{

	public static final String CATA = "catagoryname";
	public static final String NUMANSWER = "answernumber";
	public static final String SYNC = "synchronized";
	
	
	private Spinner spinnerLevel;
	private EditText enternumanswer;
	private Button btnSave;
	private CheckBox isSync;
	
	private ArrayAdapter<String> cataadapter;
	private ArrayList<CatagoryE> arraylistcatagory;
	private ArrayList<String> arrlistcata;
	private CatagoryE cataitem;
	QuestionData questiondata;
	
	private int numAnswer = 4;
	private String catagoryname;
	private Boolean sync = false;
	private SharedPreferences sharePre;
	private Context context;
	private Editor edit;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settinglayout);
		
		context = this;
		sharePre = context.getSharedPreferences("InsertQuestion", Context.MODE_PRIVATE);
		edit = sharePre.edit();
		
		spinnerLevel = (Spinner)findViewById(R.id.spinner_Level_sharereferent);
		enternumanswer = (EditText)findViewById(R.id.editText_NumAnswer_sharereferent);
		btnSave = (Button)findViewById(R.id.btn_Save_Sharepreferent);
		isSync = (CheckBox)findViewById(R.id.checkBox_synchronized);
		
		arraylistcatagory = new ArrayList<CatagoryE>();
		arrlistcata = new ArrayList<String>();
		
		showLevel();
		
		//spinnerLevel.setSelection(0);
		
		
		spinnerLevel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				catagoryname = spinnerLevel.getSelectedItem().toString();
				System.out.println("catagoryname  = = "+catagoryname );
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		isSync.setOnCheckedChangeListener(onCheck_Sync);
		
		LoadSharePreferences();
		btnSave.setOnClickListener(onClick_btn_Save);
	}
	
	private android.widget.CompoundButton.OnCheckedChangeListener onCheck_Sync = new android.widget.CompoundButton.OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			sync = isChecked;
			
		}
	};
	
	
	private android.view.View.OnClickListener onClick_btn_Save = new android.view.View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			try {
				
				numAnswer = Integer.parseInt(enternumanswer.getText().toString());
				catagoryname = spinnerLevel.getSelectedItem().toString();
				edit = sharePre.edit();
				
				edit.putString(CATA, catagoryname);
				edit.putInt(NUMANSWER, numAnswer);
				edit.putBoolean(SYNC, sync);
				edit.commit();
				
				System.out.println(catagoryname+ " va "+ numAnswer + " save ok!");
				//LoadSharePreferences();
			} catch (Exception e) {
				System.out.println("Can't save");
			}
			SettingActivity.this.finish();
		}
	};
	
	private void setPositionSpinner(){
		for(int i=0;i<arraylistcatagory.size();i++)	{
			if(spinnerLevel.getItemAtPosition(i).toString().equals(catagoryname)){
				spinnerLevel.setSelection(i);
				System.out.println("spinner_Level.setSelection(i);  "+i);
			}
		}
	}
	
	private void LoadSharePreferences(){
		
		SharedPreferences sharedPreferences = getSharedPreferences("InsertQuestion", MODE_PRIVATE);
		//sharedPreferences.registerOnSharedPreferenceChangeListener(this);
		numAnswer = sharedPreferences.getInt(NUMANSWER, 4);
		catagoryname = sharedPreferences.getString(CATA, spinnerLevel.getSelectedItem().toString());
		enternumanswer.setText(String.valueOf(numAnswer));
		sync = sharedPreferences.getBoolean(SYNC, sync);
		isSync.setChecked(sync);
		
		setPositionSpinner();
		System.out.println("num = "+ numAnswer +" , cataname "+catagoryname );
	}
	
	private void getCatagory(){
		questiondata = new QuestionData(this);
		
		arraylistcatagory = questiondata.getAllCatagory();
		for (int i = 0; i < arraylistcatagory.size(); i++) {
			cataitem = arraylistcatagory.get(i);
			String level = cataitem.getCatagoryName();
			
			arrlistcata.add(level);
		}
		
		cataadapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arrlistcata);
		cataadapter.notifyDataSetChanged();
		cataadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		System.out.println(cataadapter.getCount());
	}
	private void showLevel(){
		getCatagory();
		spinnerLevel.setAdapter(cataadapter);
		
	}
}
